package com.javablog.repository;

import com.javablog.model.Article;
import com.javablog.model.Category;
import com.javablog.model.Tag;
import com.javablog.model.User;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface ArticleRepository extends JpaRepository<Article, Long> {
    
    Page<Article> findByAuthor(User author, Pageable pageable);
    
    Page<Article> findByCategory(Category category, Pageable pageable);
    
    Page<Article> findByTagsContaining(Tag tag, Pageable pageable);
    
    @Query("SELECT a FROM Article a WHERE a.title LIKE %:keyword% OR a.content LIKE %:keyword%")
    Page<Article> searchByKeyword(@Param("keyword") String keyword, Pageable pageable);
    
    @Query("SELECT a FROM Article a ORDER BY a.createdAt DESC")
    Page<Article> findLatest(Pageable pageable);
    
    @Query("SELECT a FROM Article a ORDER BY a.views DESC")
    Page<Article> findMostViewed(Pageable pageable);
    
    @Query("SELECT a FROM Article a ORDER BY a.likes DESC")
    Page<Article> findMostLiked(Pageable pageable);
} 